package main
import (
	"fmt"
)
//约瑟夫问题
// n个人围成一圈，从第k个人开始。每数到m就停，该人出列，然后下一次重新从1开始数，重复操作直到最后一个人

func main(){
	var n,k,m int
	fmt.Scanln(&n)
	fmt.Scanln(&k)
	fmt.Scanln(&m)
	head := &PersonNode{}
	for i:=1;i<=n;i++{  //存入数据
		pn := &PersonNode{
			no : i,
		}
		PushLink(head,pn)
	}
	//遍历链表测试
	ListCircleLink(head)
	temphead := head
	for i:=0;i<k-1;i++{  //将当前结点移动到开始报数的小孩
		temphead = temphead.next
	}
	fmt.Printf("\n从第%d个小孩人开始\n",temphead.no)
	for {
		if temphead.next == temphead{ //如果环形链表就剩一个小孩就退出
			break
		}
		for j:=0;j<m-1;j++{	//开始报数m-1下
			temphead = temphead.next
		}
		temp := temphead.next
		head = Pop(head,temphead.no)  //将数到m的小孩移除环形链表
		fmt.Println("移除Person",temphead.no)
		temphead = temp 	//将当前结点指向下一个小孩然后重新开始数
	}
	fmt.Println("最后剩下的是编号：",temphead.no)	
	ListCircleLink(head)
}

type PersonNode struct{
	no int
	next *PersonNode
}

func PushLink(head *PersonNode,newNode *PersonNode){
	if head.next == nil {
		head.no = newNode.no
		head.next = head
		//fmt.Println("加入了第一个人")
		return
	}
	temp := head
	for {
		if temp.next == head{
			break
		}
		temp = temp.next
	}
	temp.next = newNode
	newNode.next = head
}

func Pop(head *PersonNode,no int)*PersonNode{
	temp := head
	temp2 := head//用于指向最后一个元素
	//不需要考虑只有一只人的情况
	for {
		if temp2.next == head{
			break
		}
		temp2 = temp2.next
	}
	for {
		if temp.no == no{
			if temp == head{
				head = head.next
			}
			temp2.next = temp.next
			return head
		}
		if temp.next == head{
			break
		}
		temp = temp.next
		temp2 = temp2.next
	}
	return head
}

//遍历所有小孩
func ListCircleLink(head *PersonNode){
	temp := head
	for {
		fmt.Printf("Person%d->",temp.no)
		if temp.next == head{
			break
		}
		temp = temp.next
	}
}